<?php

/**
 * 通用功能模块 globals Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: globals.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class globals extends Model
{

    private $class_table;
    private $article_table;
    private $city_table;
    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->class_table = '#@__article_class';
        $this->article_table = '#@__article';
        $this->city_table = '#@__city';
        $this->ad_table = '#@__ad';
    }

    /**
     * 由pid 或 cityid得到city的info
     * @param <type> $id
     * @param <type> $type 默认p的时候就是pid $type = 'c' cityid得到
     * @return <type> 
     */
    public function getCityinfo($id, $type = 'p')
    {
        if ($type == 'p') {
            $sql = "SELECT * FROM $this->city_table WHERE province = $id LIMIT 0,1";
        } elseif ($type == 'pid') {
            $sql = "SELECT * FROM $this->city_table WHERE province = 1 AND pid = $id LIMIT 0,1";
        } else {
            $sql = "SELECT * FROM $this->city_table WHERE cityid = $id LIMIT 0,1";
        }
        $rs = $this->db->cacheGetRow($sql);
        return $rs;
    }

    /**
     * 删除非空文件夹
     * @param $dir;
     * return
     */
    public function deldir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->deldir($fullpath);
                }
            }
        }

        closedir($dh);

        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 从数据库中读取广告
     * @param <type> $id
     * @param <type> $css
     */
    public function ShowAD($id, $css)
    {
        $sql = "SELECT * FROM $this->ad_table WHERE ad_id = $id AND ad_state_radio<>3 LIMIT 0,1";
        $rs = $this->db->cacheGetRow($sql, 60);
        if ($rs) {
            //如果状态是正常 就开始判断开始和结束时间
            if ($rs['ad_state_radio'] == 1) {
                if ($rs['ad_starttime'] > time() || $rs['ad_endtime'] < time()) {
                    $ad_body = null;
                    $this->assign('ad_body', $ad_body);
                    return $ad_body;
                }
            }
            switch ($rs['ad_type_radio'])
            {
                case 1:
                    $ad_body = "<div class='{$css}'><a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'>{$rs['ad_title']}</a></div>";
                    break;
                case 2:
                    $ad_body = "<div class='{$css}'><a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'><img width='{$rs['ad_width']}' height='{$rs['ad_height']}' src='{$rs['ad_uploadfile']}' border='0' alt='{$rs['ad_title']}' /></a></div>";
                    break;
                case 3:
                    $ad_body = "<div class='{$css}'>{$rs['ad_externallinks']}</div>";
                    break;
            }
        } else {
            $ad_body = null;
        }
        $this->assign('ad_body', $ad_body);
        return $ad_body;
    }

    /**
     * 取默认城市信息
     * @return <type>
     */
    public function getCfgcity()
    {
        $cityid = $GLOBALS['cfg_indexCitylist'];
        $cityid = explode(',', $cityid);
        $cityid = explode('|', $cityid[0]);
        $cityname = $cityid[1];
        $cityinfo = array('cityid' => $cityid[0], 'cityname' => $cityid[1]);
        return $cityinfo;
    }

    /**
     * 取星级
     * @param <type> $rank 
     */
    public function hotelranknamenum($rank)
    {
        if ($rank > 6 and $rank < 17) {
            $hotelranknamenum = 2; //经济型
        } elseif ($rank > 4 and $rank < 7) {
            $hotelranknamenum = 3; //舒适型
        } elseif ($rank > 2 and $rank < 5) {
            $hotelranknamenum = 4; //高档型
        } elseif ($rank > 0 and $rank < 3) {
            $hotelranknamenum = 5; //豪华型
        }
        echo $hotelranknamenum;
    }

    public function hotelrankname($rank)
    {
        If ($rank > 6 and $rank < 17) {
            $hotelrankname = "二星级"; //经济型
        } ElseIf ($rank > 4 and $rank < 7) {
            $hotelrankname = "三星级"; //舒适型
        } ElseIf ($rank > 2 and $rank < 5) {
            $hotelrankname = "四星级"; //高档型
        } ElseIf ($rank > 0 and $rank < 3) {
            $hotelrankname = "五星级"; //豪华型
        }
        echo $hotelrankname;
    }

}